package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.acticle.domain.AswerContent;
import cn.wolfcode.luowowo.acticle.domain.AswerDetail;
import cn.wolfcode.luowowo.acticle.domain.AswerTag;
import cn.wolfcode.luowowo.acticle.domain.Destination;
import cn.wolfcode.luowowo.acticle.query.AswerQueryObject;
import cn.wolfcode.luowowo.acticle.service.IAswerService;
import cn.wolfcode.luowowo.acticle.service.IDestinationService;
import cn.wolfcode.luowowo.acticle.vo.AswerStatisVo;
import cn.wolfcode.luowowo.cache.service.IAswerRedisService;
import cn.wolfcode.luowowo.comment.domain.AnswerComment;
import cn.wolfcode.luowowo.comment.domain.RemarkComment;
import cn.wolfcode.luowowo.comment.service.IAnswerCommentService;
import cn.wolfcode.luowowo.comment.service.IRemarkCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.service.IAnserTemplateService;
import cn.wolfcode.luowowo.search.template.AnswerTemplate;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("answer")
public class AnSwerController {
    @Reference
    private IAswerService answerService;
    @Reference
    private IAswerRedisService aswerRedisService;
    @Reference
    private IAnswerCommentService answerCommentService;
    @Reference
    private IRemarkCommentService remarkCommentService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IAnserTemplateService anserTemplate;

    //查询社区回答明细
    @RequestMapping("detail")
    public String detail(Model model,AnswerComment answerComments){
        List<AnswerComment> comment = answerCommentService.getAnswerComment(Long.valueOf(answerComments.getTitleId()));
        List<Map<String, Object>> list = new ArrayList<>();
        for (AnswerComment answerComment : comment) {
            Map<String, Object> map = new HashMap<String, Object>();
            if(answerComments.getUserId()==null){
                List<RemarkComment> remark=remarkCommentService.getRemarkAll(answerComment.getUserId());;
                if(remark!=null){
                    map.put("remarks",remark);
                }
            }else {
                List<RemarkComment> remark=remarkCommentService.getRemarkAll(answerComments.getUserId());;
                map.put("remarks",remark);
            }
            map.put("comment",answerComment);
            list.add(map);
            AswerDetail unAwers = this.getUnAwers(answerComment);
            model.addAttribute("comment",unAwers);
        }
        model.addAttribute("list",list);
        return "/answer/comment/detail";
    }
    //评论信息
    @RequireLogin
    @RequestMapping("remark")
    @ResponseBody
    public AjaxResult saveOrremark(RemarkComment remark, @UserParam UserInfo userInfo){
        if(userInfo==null){
            return new AjaxResult("请登录重试");
        }
        remark.setUserInfoId(userInfo.getId());
        remark.setUserInfoName(userInfo.getNickname());
        remark.setUserInfoHeadImgUrl(userInfo.getHeadImgUrl());
        remarkCommentService.save(remark);
        return new AjaxResult();
    }
    //获取评论信息
    @RequestMapping("getRemark")
    public String getRemark(Model model,Long uid){
        List<RemarkComment> remark=remarkCommentService.getRemarkAll(uid);
        List list = new ArrayList();
        list.add(remark);
        model.addAttribute("remarks",list);
        return "/answer/detailTpl";
    }
    //es根据关键字搜索信息
    @RequestMapping("estitle")
    public String getAnswerByTitle(Model model,String key){
        List<AnswerTemplate> byTtile = anserTemplate.getAnswerByTtile(key);
        ArrayList list = new ArrayList();
        for (AnswerTemplate answerTemplate : byTtile) {
            List<AswerTag> list1 = new ArrayList();
            ArrayList arrayList = new ArrayList();
            AswerDetail detail = new AswerDetail();

            //阅读数
            detail.setRead(answerTemplate.getRead());
            //评论数
            detail.setRemark(answerTemplate.getRemark());
            //顶
            detail.setTop(answerTemplate.getTop());
            //问题图片
            detail.setCoverUrl(answerTemplate.getCoverUrl());
            //创建时间
            detail.setCrateTime(answerTemplate.getCrateTime());
            detail.setState(0L);
            detail.setTitle(answerTemplate.getTitle());
            detail.setTitleId(answerTemplate.getTitleId());
            detail.setId(detail.getId());
            //目的地
            Destination dest = new Destination();
            Long destId = answerTemplate.getDestId();
            dest.setId(destId);
            dest.setName(answerTemplate.getCity());
            detail.setDest(dest);
            //用户信息
            UserInfo userInfo = new UserInfo();
            userInfo.setId(answerTemplate.getUserId());
            userInfo.setHeadImgUrl(answerTemplate.getHeadImgUrl());
            userInfo.setNickname(answerTemplate.getNickname());
            userInfo.setCity(answerTemplate.getDetailName());
            userInfo.setCity(answerTemplate.getCity());
            detail.setUserInfo(userInfo);
            //设置文本
            AswerContent content = new AswerContent();
            content.setContext(answerTemplate.getContext());
            detail.setContent(content);
            //标签
            List<String> tag = answerTemplate.getAswerTag();
            for (String s : tag) {
                AswerTag aswerTag = new AswerTag();
                aswerTag.setName(s);
                list1.add(aswerTag);//返回成功是boolean
            }

            detail.setAswerTag(list1);
            list.add(detail);
        }
        model.addAttribute("comment",list);
        return "/answer/answer";
    }


    //最热回答页面
    @RequestMapping("list")
    public String  list(Model model, @ModelAttribute("qo") AswerQueryObject qo){
        List<AswerDetail> list = answerService.list();
        List  asw= new ArrayList<>();
        AswerDetail detail = new AswerDetail();
        for (AswerDetail aswerDetail : list) {
            AswerStatisVo aswVo = aswerRedisService.getAswVo(aswerDetail.getUserInfo().getId());
            if (aswVo.getUserInfoId() != null) {
                aswerDetail.setTop(aswVo.getTop());
                aswerDetail.setRead(aswVo.getRead());
                aswerDetail.setRemark(aswVo.getRemark());

            }
            asw.add(aswerDetail);
        }
        Collections.reverse(asw);
        model.addAttribute("comment",asw);
        return "/answer/answer";
    }
    //控制最新最热
    @RequestMapping("templates")
    public String  templates(Model model, @ModelAttribute("qo") AswerQueryObject qo){
        Integer type = qo.getType();
        if(type==1){
             getNewAnswer(model,qo);
        }else if(type==2){
             getUnAwer(model,qo);
        }else if(type==0){
             getHotAnswer(model,qo);
        }

        return "/answer/answerTpl";
    }
    //最热回答
    private void getHotAnswer(Model model,@ModelAttribute("qo") AswerQueryObject qo) {
        List<AswerDetail> list = answerService.list();
        List asw = new ArrayList<>();
        AswerDetail detail = new AswerDetail();
        for (AswerDetail aswerDetail : list) {
            AswerStatisVo aswVo = aswerRedisService.getAswVo(aswerDetail.getUserInfo().getId());
            if (aswVo.getUserInfoId() != null) {
                aswerDetail.setTop(aswVo.getTop());
                aswerDetail.setRead(aswVo.getRead());
                aswerDetail.setRemark(aswVo.getRemark());
            }
            asw.add(aswerDetail);
        }
        Collections.reverse(asw);
        model.addAttribute("comment",asw);
    }

    //待回答信息
    private void getUnAwer(Model model,@ModelAttribute("qo") AswerQueryObject qo) {
        List<AnswerComment> list = answerCommentService.list(qo);
        List<AswerTag> list1 = new ArrayList();
        ArrayList arrayList = new ArrayList();
        for (AnswerComment answerComment : list) {
            for(int i=0;i<list.size();i++){
                AswerDetail unAwers = this.getUnAwers(answerComment);
                arrayList.add(unAwers);
                break;
            }
        }
        model.addAttribute("comment",arrayList);
    }
    //最新问题页面
    private void getNewAnswer(Model model,@ModelAttribute("qo") AswerQueryObject qo) {
        List<AnswerComment> list = answerCommentService.list(qo);
        ArrayList arrayList = new ArrayList();
        for (AnswerComment answerComment : list) {
            for(int i=0;i<list.size();i++){
                AswerDetail unAwers = this.getUnAwers(answerComment);
                arrayList.add(unAwers);
                break;
            }
        }
        model.addAttribute("comment",arrayList);
    }

    //跳转到提问页面
    @RequestMapping("input")
    public String input(Model model){
        model.addAttribute("destination",destinationService.getDestinationByDeepIs3());
        return "/answer/input";
    }
    //保存提问信息
    @RequireLogin
    @RequestMapping("saveOrupdate")
    @ResponseBody
    public AjaxResult saveOrupdate(AnswerComment answerComment, String[] aswerTag,@UserParam UserInfo userInfo){
        answerComment.setUserId(userInfo.getId());
        answerComment.setNickname(userInfo.getNickname());
        answerComment.setHeadImgUrl(userInfo.getHeadImgUrl());
        answerComment.setCity(userInfo.getCity());
        answerCommentService.save(answerComment,aswerTag);
        return new AjaxResult();
    }
    //顶: 即点赞
    @ResponseBody
    @RequestMapping("/answerTop")
    public AjaxResult strategyThumbup(Long uid, @UserParam UserInfo u,Model model){
        if(u == null){
            return new AjaxResult("请先登录再顶", AjaxResult.CODE_TYPE_UNLOGIN);
        }
        boolean ret = aswerRedisService.top(uid);
        AswerStatisVo vo = aswerRedisService.getAswVo(uid);
        System.out.println(vo.getTop());
        return new AjaxResult(ret, "").addData(aswerRedisService.getAswVo(uid));
    }
    private AswerDetail getUnAwers(AnswerComment answerComment){
        List<AswerTag> list1 = new ArrayList();
        ArrayList arrayList = new ArrayList();
        AswerDetail detail = new AswerDetail();

        //阅读数
        detail.setRead(answerComment.getRead());
        //评论数
        detail.setRemark(answerComment.getRemark());
        //顶
        detail.setTop(answerComment.getTop());
        //问题图片
        detail.setCoverUrl(answerComment.getCoverUrl());
        //创建时间
        detail.setCrateTime(answerComment.getCrateTime());
        detail.setState(0L);
        detail.setTitle(answerComment.getTitle());
        detail.setTitleId(answerComment.getTitleId());
        detail.setId(detail.getId());
        //目的地
        Destination dest = new Destination();
        Long destId = answerComment.getDestId();
        dest.setId(destId);
        dest.setName(answerComment.getCity());
        detail.setDest(dest);
        //用户信息
        UserInfo userInfo = new UserInfo();
        userInfo.setId(answerComment.getUserId());
        userInfo.setHeadImgUrl(answerComment.getHeadImgUrl());
        userInfo.setNickname(answerComment.getNickname());
        userInfo.setCity(answerComment.getDetailName());
        userInfo.setCity(answerComment.getCity());
        detail.setUserInfo(userInfo);
        //设置文本
        AswerContent content = new AswerContent();
        content.setContext(answerComment.getContext());
        detail.setContent(content);
        //标签
        List<String> tag = answerComment.getAswerTag();
        for (String s : tag) {
            AswerTag aswerTag = new AswerTag();
            aswerTag.setName(s);
            list1.add(aswerTag);//返回成功是boolean
        }

        detail.setAswerTag(list1);
        return detail;
    }


}
